package org.zjut.db;

import com.alibaba.fastjson.JSON;
import com.sun.xml.internal.bind.v2.TODO;
import com.sun.xml.internal.ws.api.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zjut.pso.PSOBase;
import org.zjut.pso.PSOone;
import org.zjut.util.CommonUtil;

import javax.rmi.CORBA.Tie;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Rising
 * @date 2017/8/22
 * @des 学习者属性赋值
 */
public class LearnerCourseAttrDataBase extends JdbcDaoSupport {
    String dataBase = "pso_recommond";
    String tb_LearnerCourseAttr = ".attr_learner_course";
    String view_learnerCourseAttr = ".view_user_course_attr";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    LearnConceptDataBase mLearnConceptDataBase;
    @Autowired
    ResourceDataBase mResourceDataBase;
    @Autowired
    PSOBase psoBase;

    /**
     * 新增学习者与课程之间的属性
     * @param params
     * @return
     */
    @Transactional(rollbackFor = java.lang.Exception.class)
    public Map<String,Object> addCourseAttr(Map<String,Object> params) throws Exception{
        String userCode = null, courseCode = null;
        List<Map> valueInfo = null;     //属性信息
        Map<String,Object> result = new HashMap<>();

        try {
            userCode = params.get("userCode").toString();
            courseCode = params.get("courseCode").toString();
            valueInfo =  JSON.parseArray(params.get("valueInfo").toString(),Map.class) ;
        }catch (Exception e){
            return CommonUtil.setErrorInfo("请检查参数");
        }

        String sql = "INSERT INTO "+dataBase+tb_LearnerCourseAttr+" (learnerCode,courseCode,attrCode,attrValue,serverTime) VALUES(?,?,?,?,?)";
        String time = sdf.format(new Date());
        if(valueInfo!=null&&valueInfo.size()>0){
            //应该先删除原先的对应关系，然后重新插入
            delCourseAttr(params);
            try {
                for (Map<String,Object> item : valueInfo){
                    getJdbcTemplate().update(sql,new Object[]{userCode,courseCode,item.get("attrCode").toString(),
                            item.get("attrValue").toString(), time});
                }
                //算法参数可以自己重新定义
                //必须初始化，决定使用哪种推荐算法
                //这里用PSOone算法，只是个demo
                psoBase.setmPSO(new PSOone(mLearnConceptDataBase,mResourceDataBase));
                psoBase.saveRecommondKnowledge(params);

                result.put("success",true);
                result.put("message","生成成功");
                return result;
            }catch (Exception e){
                System.out.println(e.getMessage()+" addCourseAttr");
                throw e;
            }
        }else{
            return CommonUtil.setErrorInfo("valueInfo 参数为空");
        }
    }
    /**
     * 修改学习者与课程之间的属性
     * @param params
     * @return
     */
    @Transactional(rollbackFor = java.lang.Exception.class)
    public Map<String,Object> editCourseAttr(Map<String,Object> params){
        return null;
    }
    /**
     * 获取学习者与课程之间的属性
     * @param params
     * @return
     */
    public Map<String,Object> getCourseAttr(Map<String,Object> params){
        String userCode = null, courseCode = null;
        Map<String,Object> result = new HashMap<>();
        try {
            userCode = params.get("userCode").toString();
            courseCode = params.get("courseCode").toString();
        }catch (Exception e){
            return CommonUtil.setErrorInfo("请检查参数");
        }
        String sql = "SELECT * FROM "+dataBase+view_learnerCourseAttr+" as a WHERE " +
                " a.learnerCode =?  and a.courseCode = ? ";
        try {
            List<Map<String,Object>> courseAttrs = getJdbcTemplate().queryForList(sql,new Object[]{userCode,courseCode});
            Map<String,Object> message = new HashMap<>();
            message.put("valueInfo",courseAttrs);
            message.put("userCode",userCode);
            message.put("courseCode",courseCode);
            result.put("success",true);
            result.put("message",courseCode);
        }catch (Exception e){
            return CommonUtil.setErrorInfo("查询失败");
        }
        return result;
    }
    /**
     * 删除学习者与课程之间的属性
     * @param params
     * @return
     */
    public Map<String,Object> delCourseAttr(Map<String,Object> params){
        String userCode = null , courseCode = null;
        Map<String,Object> result = new HashMap<>();
        try {
            userCode = params.get("userCode").toString();
            courseCode = params.get("courseCode").toString();
        }catch (Exception e){
            return CommonUtil.setErrorInfo("请检查参数");
        }

        String sql = "delete from "+dataBase+tb_LearnerCourseAttr+" where learnerCode = ? and courseCode = ?";
        try {
            getJdbcTemplate().update(sql,new Object[]{userCode,courseCode});
            result.put("success",true);
            result.put("message","删除成功");
        }catch (Exception e){
            throw  e;
        }
        return result;
    }



//    /**
//     * 新增学习者属性值
//     * @param params
//     * @return
//     */
//    @Transactional(propagation= Propagation.REQUIRED)
//    public Map<String,Object> addLeanerAttr(Map<String,Object>params){
//        Map<String,Object> result  = new HashMap<>();
//        String userCode = null ;
//        List<Map> valueInfo = null;
//        try{
//            userCode = params.get("userCode").toString();
//            System.out.println(params.get("valueInfo").toString());
//            valueInfo =  JSON.parseArray(params.get("valueInfo").toString(),Map.class) ;
//        }catch (Exception e){
//            return CommonUtil.setErrorInfo("请检查参数");
//        }
//
//        String sql = "INSERT INTO "+ dataBase+".leaner_attr (code,userCode,attrCode,attrValue) VALUES(?,?,?,?) ";
//        if(valueInfo!=null&&valueInfo.size()>0){
//            try{
//                for (Map<String,Object> item : valueInfo){
////                生成配置表主键
//                    String code = UUID.randomUUID().toString();
//                    getJdbcTemplate().update(sql,new Object[]{code,userCode,item.get("attrCode").toString(),item.get("attrValue").toString()});
//                }
//                result.put("success",true);
//                result.put("message","新增成功");
//            }catch (Exception e){
//                throw e;
//            }
//        }else {
//            return CommonUtil.setErrorInfo("请检查参数");
//        }
//
//        return result;
//    }
//
//    /**
//     * 查询学习者属性值
//     * @param params
//     * @return
//     */
//    public Map<String,Object> getLeanerAttr(Map<String,Object> params){
//        Map<String,Object> result = new HashMap<>();
//        String userCode = null;
//        String sql = "SELECT * FROM "+dataBase+".view_user_info ";
//
//        if(params.containsKey("userCode")){
//            userCode = params.get("userCode").toString();
//            sql = sql + " where userCode = ? ";
//        }else{
//            return CommonUtil.setErrorInfo("请检查参数");
//        }
//
//        try{
//            List<Map<String,Object>> leanerAttrInfo = new ArrayList<>();
//            List<Map<String,Object>> userInfo = getJdbcTemplate().queryForList(sql,new Object[]{userCode});
//            if(userInfo!=null){
//                for(Map<String,Object> item : userInfo){
//                    String sql2 = " SELECT * FROM "+dataBase+".view_leaner_attr as a where a.userCode = ? ";
//                    List<Map<String,Object>> attrs = getJdbcTemplate().queryForList(sql2,new Object[]{item.get("userCode".toString())});
//                    item.put("valueInfo",attrs);
//                    leanerAttrInfo.add(item);
//                }
//
//            }
//            Map<String,Object> message  = new HashMap<>();
//            message.put("leanerAttrInfo",leanerAttrInfo);
//            result.put("success",true);
//            result.put("message",message);
//            return result;
//        }catch (Exception e){
//            return CommonUtil.setErrorInfo("查询失败");
//        }
//    }
//
//    /**
//     * 修改学习者属性值
//     * @param params
//     * @return
//     */
//    @Transactional(propagation= Propagation.REQUIRED)
//    public Map<String,Object> editLeanerAttr(Map<String,Object> params){
//        Map<String,Object> result = new HashMap<>();
//        String userCode = null ;
//        List<Map> valueInfo = null;
//        try{
//            userCode = params.get("userCode").toString();
//            valueInfo =  JSON.parseArray(params.get("valueInfo").toString(),Map.class) ;
//        }catch (Exception e){
//            return CommonUtil.setErrorInfo("请检查参数");
//        }
//        try {
//            //先删掉原来对应的属性值
//            Map<String, Object> delParam = new HashMap<String, Object>();
//            delParam.put("userCode", userCode);
//            delLeanerAttr(delParam);
//            //然后再添加修改的值
//            result = addLeanerAttr(params);
//        }catch (Exception e){
//            throw e;
//        }
//        return result;
//    }
//
//    /**
//     * 清除学习者属性值
//     * @param params
//     * @return
//     */
//    @Transactional(propagation= Propagation.REQUIRED)
//    public Map<String,Object> delLeanerAttr(Map<String,Object> params){
//        Map<String,Object> result = new HashMap<>();
//        String userCode = null;
//        try{
//            userCode = params.get("userCode").toString();
//        }catch (Exception e){
//            return CommonUtil.setErrorInfo("请检查参数");
//        }
//
//        String sql = "DELETE FROM "+dataBase+".leaner_attr WHERE userCode = ? ";
//
//        try{
//            getJdbcTemplate().update(sql,new Object[]{userCode});
//        }catch (Exception e){
//            throw e;
//        }
//
//        result.put("success",true);
//        result.put("message","删除成功");
//        return result;
//    }
}
